package com.lyn.demo;

import com.feizhou.swagger.annotation.Api;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lyn.common.base.Result;
import com.lyn.domain.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tag = "api.v1.batch", description = "测试批量插入")
public class BatchController extends Controller {
    /**
     * 批量插入数据，需要sql，采用二维数组存储参数值
     * 测试通过
     */
    public void testBatchInsert1() {
        List<User> srcList = initSrc();
        String sql = "insert into user(user_id, nick_name, email, password) values(?,?,?,?)";
        Object[][] param = new Object[srcList.size()][4];
        for(int i=0; i<param.length; i++) {
            User user = srcList.get(i);
            param[i][0] = user.getUserId();
            param[i][1] = user.getNickName();
            param[i][2] = user.getEmail();
            param[i][3] = user.getPassword();
        }
        int[] batch = Db.batch(sql, param, 2);
        System.out.println(Arrays.toString(batch));
        renderJson(Result.ok("批量插入成功"));
    }

    /**
     * 批量插入数据，需要sql，采用List<User>存储参数值
     * 测试通过
     */
    public void testBatchInsert2() {
        List<User> srcList = initSrc();
        String sql = "insert into user(user_id, nick_name, email, password) values(?,?,?,?)";
        String columns = "user_id, nick_name, email, password";

        int[] batch = Db.batch(sql, columns, srcList, 100);
        System.out.println(Arrays.toString(batch));
        renderJson(Result.ok("批量插入成功"));
    }

    /**
     * 批量插入数据，无需sql，无需声明表名，采用List<User>存储参数值
     * 测试通过 推荐
     */
    public void testBatchInsert3() {
        List<User> srcList = initSrc();
        int[] ints = Db.batchSave(srcList, 100);
        System.out.println(Arrays.toString(ints));
        renderJson(Result.ok("批量插入成功"));
    }

    /**
     * 批量插入数据，无需sql，需声明表名，采用List<Record>存储参数值
     * 测试通过
     */
    public void testBatchInsert4() {
        List<Record> srcList = initRecord();
        int[] ints = Db.batchSave("user", srcList, 100);
        System.out.println(Arrays.toString(ints));
        renderJson(Result.ok("批量插入成功"));
    }

    private List<User> initSrc() {
        List<User> srcList = new ArrayList<>();
        for(int i=1; i<=10; i++) {
            String header = "batch"+i;
            User user = new User();
            user.setUserId(header);
            user.setNickName(header+"Nick");
            user.setEmail(header+"@qq.com");
            //user.setPassword(header+"_123");
            srcList.add(user);
        }
        return srcList;
    }

    private List<Record> initRecord() {
        List<Record> srcList = new ArrayList<>();
        for(int i=1; i<=10; i++) {
            String header = "batch"+i;
            Record ret = new Record();
            ret.set("user_id", header);
            ret.set("nick_name", header+"Nick");
            ret.set("email", header+"@qq.com");
            srcList.add(ret);
        }
        return srcList;
    }

}
